<html>
<body>
<?php

include 'dbconnect.php';
	
if ( isset($_POST['act_bookmark']) )
	{
		
		
		$vId = $_POST['vehicle_id'];
		$uName = $_POST['user_name'];
		$time = gettimeofday(true);
				
		$existsQuery = "select count(*) from Bookmark where
			RegisteredUser_NonAdmins_AllUsers_LoginID = '$uname' and
			VehicleForSale_VehicleForSaleID = $vId";
		
		$result = mysqli_query( $mysql, $existsQuery );
		if(!$result)
			exit;
		
		$row = mysqli_fetch_row( $result );
		$count = $row[0];
		
		// bookmark doesnt exist add one
		if( count <= 0 )
		{
			$insertQuery = "insert into Bookmark( RegisteredUser_NonAdmins_AllUsers_LoginID 
				,VehicleForSale_VehicleForSaleID, Timestamp ) 
				values ( '$uName', '$vId', '$time')";
			
			$result = mysqli_query( $mysql, $insertQuery );
			if(!$result)
			{
				echo 'Error: failed to create bookmark';
				exit;
			}
			
			echo 'Bookmark Created for Vehicle </br>';
			echo '<a href=download.php?id='.$vId.'&name='.$uName.'> Return to listing </a></br>';
			
			$query = "select * from AllUsers where LoginID = '$uName'";
			$result = mysqli_query( $mysql, $query);
			if(!$result)
				exit;
			
			$row = mysqli_fetch_assoc( $result );
			$pass = $row["UserPassword"];
			
			echo'<form enctype="multipart/form-data" action="login.php" method="POST">
			<input type="submit" value="Return To Profile">
			<input type="hidden" name="name" value="'.$uName.'">
			<input type="hidden" name="password" value="'.$pass.'">
			</form>';
		}
	}
elseif ( isset($_POST['act_bookmark_delete']))
	{
		$uName = $_POST['user_name'];
		$vId = $_POST['vehicle_id'];
		
		$query = "delete from Bookmark where
			RegisteredUser_NonAdmins_AllUsers_LoginID = '$uName' and
			VehicleForSale_VehicleForSaleID = $vId";
		
		$result = mysqli_query( $mysql, $query);
		if(!$result)
		{
			echo 'Error: Failed to delete bookmark';
			exit;
		}
		
		$query = "select * from AllUsers where LoginID = '$uName'";
		$result = mysqli_query( $mysql, $query);
		if(!$result)
			exit;
		
		$row = mysqli_fetch_assoc( $result );
		$pass = $row["UserPassword"];
		
		echo 'Bookmark Deleted </br>';
		echo'<form enctype="multipart/form-data" action="login.php" method="POST">
		<input type="submit" value="Return">
		<input type="hidden" name="name" value="'.$uName.'">
		<input type="hidden" name="password" value="'.$pass.'">
		</form>';
		
	}
elseif ( isset($_POST['act_bookmark_view']))
	{
		$uName = $_POST['user_name'];
		$time = gettimeofday(true);
		
		$query = "select * from Bookmark where
			RegisteredUser_NonAdmins_AllUsers_LoginID = '$uName'";
		
		$result = mysqli_query($mysql, $query);
		if(!$result)
		{
			echo 'foo';
			echo $uname;
			
			exit;
		}
		
		while ($row = mysqli_fetch_assoc($result)) 
		{
			
			$uName = $row["RegisteredUser_NonAdmins_AllUsers_LoginID"];
			$vId = $row["VehicleForSale_VehicleForSaleID"];

			echo'<a href="download.php?id='.$vId.'&name='.$uName.'">download.php?id='.$vId.'&name='.$uName.'</a>
			<form enctype="multipart/form-data" action="bookmark.php" method="POST">
			<input type="submit" value="Delete" name="act_bookmark_delete">
			<input type="hidden" name="user_name" value="'.$uName.'">
			<input type="hidden" name="vehicle_id" value='.$vId.'>
			</form>';
		}
	}
	
mysqli_close($mysql);
?>
</body>
</html>